<?php
namespace app\index\model;
use think\Model;
use think\Db;
use think\Session;
use think\Controller;

class User_model
{
	public function getindex()
    {
        $uid = $_SESSION['think']['username'][0]['user_id'];
        $time = date('Y-m-d H:i:s',time());
        // var_dump($uid);die;
        //记录用户操作
        $prev = Db('_user_log')
                ->where('user_id',$uid)
                ->order('create_time desc')
                ->limit(0,1)
                ->field('log_desc')
                ->find();
        $prevdata = $prev['log_desc'];

        if ($prevdata !== '个人中心') {
            //记录用户操作
            $data = [
                'user_id' => $uid,  
                'log_action' => 'User/index',   
                'log_desc' => '个人中心',   //默认空
                'create_time' => $time,
                'is_delete' => 0 //默认0
            ];
            $res = db('_user_log')
                ->insert($data);
        }
        

        $list = Db::table('cn_user')
                ->alias('cu')
                ->join('cn_user_character_relation cucr','cu.user_id = cucr.user_id')
                ->join('cn_character cc','cc.chara_id = cucr.chara_id')
                ->join('cn_menu_character_relation cmcr','cmcr.chara_id = cc.chara_id')
                ->join('cn_menu cm','cmcr.menu_id = cm.menu_id')
                ->where('cn_user.user_id',$uid)
                ->field('menu_name')
                ->select();
                // var_dump($list);die;
        //查询 当前用户的信息
        $user = Db::table('cn_user')
                ->where('user_id',$uid)
                ->field('user_name,user_pass,user_email,user_working_place,user_mobile,user_office_phone,user_remarks')
                ->find();
        $info = [
            'user' => $user,
            'list' => $list
        ];
        return $info;
    }


    public function editInfo()
    {
        $uid = $_SESSION['think']['username'][0]['user_id'];
        // var_dump($uid);die;
        $time = date('Y-m-d H:i:s',time());
        //记录用户操作
        $prev = Db('_user_log')
                ->where('user_id',$uid)
                ->order('create_time desc')
                ->limit(0,1)
                ->field('log_desc')
                ->find();
        $prevdata = $prev['log_desc'];

        if ($prevdata !== '修改个人资料') {
            //记录用户操作
            $data = [
                'user_id' => $uid,  
                'log_action' => 'User/editInfo',   
                'log_desc' => '修改个人资料',   //默认空
                'create_time' => $time,
                'is_delete' => 0 //默认0
            ];
            $res = db('_user_log')
                ->insert($data);
        }
       

        $list = Db::table('cn_user')
                ->alias('cu')
                ->join('cn_user_character_relation cucr','cu.user_id = cucr.user_id')
                ->join('cn_character cc','cc.chara_id = cucr.chara_id')
                ->join('cn_menu_character_relation cmcr','cmcr.chara_id = cc.chara_id')
                ->join('cn_menu cm','cmcr.menu_id = cm.menu_id')
                ->where('cn_user.user_id',$uid)
                ->field('menu_name')
                ->select();
        //修改当前用户的信息
       // var_dump($_POST);die;
        $email = $_POST['email'];
        $mobile = $_POST['mobile'];
        $office_phone = $_POST['office_phone'];
        $working_place = $_POST['working_place'];
        $remarks = $_POST['remarks'];

        $data = [
            'user_email' =>$email,
            'user_mobile' =>$mobile,
            'user_office_phone' =>$office_phone,
            'user_working_place' =>$working_place,
            'user_remarks' =>$remarks,
        ];

        $euser = Db::table('cn_user')
                ->where('user_id',$uid)
                ->update($data);
        // var_dump($euser);die;
        // $info = [
        //     'list' => $list,
        //     'euser' => $euser
        // ];

        return $euser;
    }

    public function editName()
    {
        $uid = $_SESSION['think']['username'][0]['user_id'];
        // var_dump($uid);die;
        $time = date('Y-m-d H:i:s',time());
        //记录用户操作
        $prev = Db('_user_log')
                ->where('user_id',$uid)
                ->order('create_time desc')
                ->limit(0,1)
                ->field('log_desc')
                ->find();
        $prevdata = $prev['log_desc'];

        if ($prevdata !== '修改用户名') {
            //记录用户操作
            $data = [
                'user_id' => $uid,  
                'log_action' => 'User/editName',   
                'log_desc' => '修改用户名',   //默认空
                'create_time' => $time,
                'is_delete' => 0 //默认0
            ];
            $res = db('_user_log')
                ->insert($data);
        }
       

        $list = Db::table('cn_user')
                ->alias('cu')
                ->join('cn_user_character_relation cucr','cu.user_id = cucr.user_id')
                ->join('cn_character cc','cc.chara_id = cucr.chara_id')
                ->join('cn_menu_character_relation cmcr','cmcr.chara_id = cc.chara_id')
                ->join('cn_menu cm','cmcr.menu_id = cm.menu_id')
                ->where('cn_user.user_id',$uid)
                ->field('menu_name')
                ->select();
        // var_dump($_POST);die;
        $confirm = md5($_POST['pwd']);
        $newname = $_POST['name'];
        $pwd = Db::table('cn_user')
                ->where('user_id',$uid)
                ->field('user_pass')
                ->find();
        // var_dump($confirm);
        // var_dump($pwd);die;
        if ($confirm = $pwd['user_pass']) {
            $data = [
                'user_name' => $newname
            ];
            $info = Db::table('cn_user')
                    ->where('user_id',$uid)
                    ->update($data);
            return $info;
        }
        return false;
    }

    public function editPwd()
    {
        $uid = $_SESSION['think']['username'][0]['user_id'];
        // var_dump($uid);die;
        $time = date('Y-m-d H:i:s',time());
        //记录用户操作
        $prev = Db('_user_log')
                ->where('user_id',$uid)
                ->order('create_time desc')
                ->limit(0,1)
                ->field('log_desc')
                ->find();
        $prevdata = $prev['log_desc'];

        if ($prevdata !== '修改密码') {
            //记录用户操作
            $data = [
                'user_id' => $uid,  
                'log_action' => 'User/editPwd',   
                'log_desc' => '修改密码',   //默认空
                'create_time' => $time,
                'is_delete' => 0 //默认0
            ];
            $res = db('_user_log')
                ->insert($data);
        }

        $list = Db::table('cn_user')
                ->alias('cu')
                ->join('cn_user_character_relation cucr','cu.user_id = cucr.user_id')
                ->join('cn_character cc','cc.chara_id = cucr.chara_id')
                ->join('cn_menu_character_relation cmcr','cmcr.chara_id = cc.chara_id')
                ->join('cn_menu cm','cmcr.menu_id = cm.menu_id')
                ->where('cn_user.user_id',$uid)
                ->field('menu_name')
                ->select();
        // var_dump($_POST);die;
        $now = $_POST['nowmima'];
        $new = $_POST['newmima'];
        $confirm = $_POST['confirmmima'];
        $mima = Db::table('cn_user')
                ->where('user_id',$uid)
                ->field('user_pass')
                ->find();
        // var_dump($mima);die;
        $mi = $mima['user_pass']; //数据库查到的现在密码
        // var_dump($now);die; 
        $begin = md5($now);  //页面填的现在密码
        $fina = md5($new);   //页面填的新密码
        // var_dump($begin);
        // var_dump($fina);
        // var_dump($mi);die;

        if ($new == $confirm) {          //二次密码不一致
           if ($begin == $mi) {          //填写密码与数据库密码不一致
               $data = [
                'user_pass' => $fina
               ];
               $info = Db::table('cn_user')
                        ->where('user_id',$uid)
                        ->update($data);
                return $info;
           }
           return false;
        }
        return false;
    }
    
}

?>